FROM ubuntu:22.04

ENV DEBIAN_FRONTEND=noninteractive
RUN sed -i 's|archive.ubuntu.com|mirrors.aliyun.com|g' /etc/apt/sources.list \
    && sed -i 's|security.ubuntu.com|mirrors.aliyun.com|g' /etc/apt/sources.list \
    && apt-get clean && apt-get update && apt-get install -y \
    curl wget git build-essential pkg-config libssl-dev \
    libgtk-3-dev libwebkit2gtk-4.1-dev libjavascriptcoregtk-4.1-dev \
    libayatana-appindicator3-dev librsvg2-dev libgdk-pixbuf2.0-dev \
    libcairo-gobject2 libpango1.0-dev \
    && curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
    && apt-get install -y nodejs \
    && npm config set registry https://registry.npmmirror.com \
    && npm install -g pnpm \
    && pnpm config set registry https://registry.npmmirror.com \
    && rm -rf /var/lib/apt/lists/*

RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"

WORKDIR /app
COPY package.json pnpm-lock.yaml ./
RUN pnpm install

COPY src-tauri/Cargo.toml src-tauri/Cargo.lock src-tauri/
RUN mkdir -p src-tauri/src && \
    echo "fn main() {}" > src-tauri/src/main.rs && \
    echo "// Dummy lib.rs for dependency caching" > src-tauri/src/lib.rs && \
    cd src-tauri && cargo fetch

COPY . .
RUN pnpm tauri build

RUN mkdir -p /output && \
    cp src-tauri/target/release/bundle/deb/*.deb /output/ || true && \
    cp src-tauri/target/release/bundle/appimage/*.AppImage /output/ || true

VOLUME ["/output"]
CMD ["ls", "-la", "/output"]
